package com.example.servlet;
import com.example.dao.NovelDao;
import com.example.dao.UserDao;
import com.example.domain.Novel;
import com.example.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
//@WebServlet("/AdminIndexServlet")
public class AdminIndexServlet extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.setCharacterEncoding("utf-8");
        int page=1;
        HttpSession session=request.getSession();
        UserDao userDao=new UserDao();
        NovelDao novelDao=new NovelDao();
        List<User> userList=userDao.getUserListByPage(page,10);
        int userCount = userDao.getUserCount();
        int novelCount = novelDao.getTotalCount();
        int reviewCount = novelDao.getReviewList().size();

        int userPageCount = (int) Math.ceil((double) userCount / 10);
        int novelPageCount = (int) Math.ceil((double) novelCount / 10);
        int reviewPageCount = (int) Math.ceil((double) reviewCount / 10);
        session.setAttribute("reviewCount",reviewCount);
        session.setAttribute("userCount",userCount);
        session.setAttribute("novelCount",novelCount);
        session.setAttribute("userPageCount",userPageCount);
        session.setAttribute("novelPageCount",novelPageCount);
        session.setAttribute("reviewPageCount",reviewPageCount);
        session.setAttribute("reviewPage",1);
        session.setAttribute("userPage",1);
        session.setAttribute("novelPage",1);
        if (userList==null){
            request.setAttribute("msg","获取用户信息失败!");
            request.getRequestDispatcher("admin_index.jsp").forward(request,response);
            return;
        }
        session.setAttribute("userList",userList);
        response.sendRedirect(request.getContextPath()+"/admin_index.jsp");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request,response);
    }
}
